import { useNoticeStore } from '@/store'

export default class SystemHandler {
    sqlite: any
    noticeStore: any
    constructor(sqlite: any) {
        this.sqlite = sqlite
        this.noticeStore = useNoticeStore()

    }

    /**
     * 获取信息
     */
    getHistoryList(userId: any, firstTime: any) {
        return new Promise((resolve, reject) => {
            // 直接查询本地历史信息
            this.sqlite.imSystem.historyList(userId, firstTime).then((list: any) => {
                resolve(list)
            })
        })
    }

    /**
     * 处理未读通知
     */
    dealUnreadAll() {
        this.sqlite.imSystem.changeIsReadAll()
        // 清空未读数
        this.noticeStore.clearSystemUnread()
        // 重置总未读记录数
        uni.$u.$imHandler.updateTabBarText()
    }

    dealUnread() {
        this.sqlite.imSystem.changeIsReadAll()
        // 清空未读数
        this.noticeStore.clearSystemUnread()

        // 重置总未读记录数
        uni.$u.$imHandler.updateTabBarText()
    }

    /**
     * 处理未读通知
     */
    dealUnreadByNewSystem(sys: any) {
        // 本地添加/更新消息
        return new Promise(resolve => {
            sys.isRead = 1
            this.sqlite.imSystem.saveOrUpdateSystem([sys])
            resolve(sys)
        })
    }
}
